#!/usr/bin/python
import matplotlib.pyplot as plt
import random
import numpy as np
import time
thetastd=50
te=2

def mean(todolist):
	return sum(todolist)/len(todolist)

def floatrange(start,stop,steps):
	return [start+float(i)*(stop-start)/(float(steps)-1) for i in range(steps)]

def geterror(pacNumber,theta,nt,miu,sigma):
	yi=[]
	for i in range(0,pacNumber):
		xi=random.gauss(miu,sigma)
		yi.append(nt+theta+xi)
	thetaEstm=mean(yi)-nt
	error=theta-thetaEstm
	return error

err5=[]
repeats=10000
avg5=[]
sig=floatrange(0.0,100.0,200)
for t in range(repeats):
	for m in sig:
		err5.append(abs(geterror(5,thetastd,te,0,m)))
	avg5.append(err5)
	err5=[]
	print "repeats ",t," done! at: ",time.asctime()
data=[[r[col] for r in avg5] for col in range(len(avg5[0]))]
resultmean=[np.mean(tt) for tt in data]
resultvar=[np.var(tt) for tt in data]
plt.subplot(211)
plt.plot(sig,resultmean,'.',label="$sigma\_mean$")
plt.plot(sig,resultvar,label="$sigma\_var$")
plt.xlabel("sigma")
plt.ylabel("error(us)")
#plt.xticks(range(0,len(result),1))
plt.legend(loc=2)
plt.subplot(212)
plt.plot(sig,resultmean,label="$sigma$")
plt.xlabel("sigma")
plt.ylabel("error(us)")
plt.show()
